import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit'
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import noteSlice from './slice/note/index'
import subjectSlice from './slice/subject/index'
import userReducer from './slice/user/index'

// 类型定义 react官方给的，能读懂就行
export type AppDispatch = typeof store.dispatch
export type RootState = ReturnType<typeof store.getState>
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action<string>>

export const store = configureStore({
  reducer: {
    user: userReducer,
    subject: subjectSlice,
    note: noteSlice
  }
})

// useDispatch 是 react-redux 库提供的一个钩子函数，它主要用于在 React 函数组件中分发（dispatch） Redux 的 action。
export const useAppDispatch = () => useDispatch<AppDispatch>()
// useSelector 是 react-redux 库中提供的一个钩子函数，主要用于在 React 函数组件中从 Redux 存储（store）里获取特定的状态数据。
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
